package http_v3

import (
	"syscall"
	"unsafe"
)

// timerfd constants
const (
	CLOCK_MONOTONIC = 1
	TFD_CLOEXEC     = 0x80000
)

// itimerspec 定时器结构体
type itimerspec struct {
	Interval syscall.Timeval // 定时器间隔
	Value    syscall.Timeval // 初始值
}

// timerfdCreate creates a timerfd using the SYS_TIMERFD_CREATE syscall
func timerfdCreate(clockId int, flags int) (int, error) {
	fd, _, err := syscall.Syscall(syscall.SYS_TIMERFD_CREATE, uintptr(clockId), uintptr(flags), 0)
	if err != 0 {
		return -1, err
	}
	return int(fd), nil
}

func timerfdSettime(fd int, flags int, newValue *itimerspec, oldValue *itimerspec) error {
	_, _, err := syscall.Syscall6(
		syscall.SYS_TIMERFD_SETTIME,
		uintptr(fd),
		uintptr(flags),
		uintptr(unsafe.Pointer(newValue)),
		uintptr(unsafe.Pointer(oldValue)),
		0, 0)
	if err != 0 {
		return err
	}
	return nil
}
